/*
 * Created by JFormDesigner on Wed May 07 08:46:58 CST 2025
 */

package com.soft.bams.client;

import com.soft.bams.entity.Account;
import com.soft.bams.exception.LoginExcepion;
import com.soft.bams.exception.NotFoundException;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.SocketException;
import javax.swing.*;

/**
 * @author 23838
 */
public class LoginPanel extends JPanel {
    private ATMClient atm;

    public LoginPanel(ATMClient atm) {
        this.atm = atm;
        initComponents();
    }

    private void Back(ActionEvent e) {
        // 切换回主面板
        CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
        cardLayout.show(this.getParent(), "mainPanel");
        // 清空当前登录界面输入
        clearLogin();
    }

    private void OK(ActionEvent e) {
        // 收集登录界面数据
        String idStr = idField.getText();
        String password = new String(passwordField.getPassword());

        // 执行登录验证
        if (idStr != null && !idStr.trim().isEmpty()) {
            Long id = Long.valueOf(idStr);
         /*   try {
                Account acc = atm.bank.login(id, password);

                if (acc != null) {  // 登录成功
                    // 同步用户信息到ATM客户端
                    atm.acc = acc;
                    // 初始化业务面板（假设businessPanel已关联）
                    atm.businessPanel.initBusiness();
                    // 切换到业务面板（通过父容器的CardLayout）
                    CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
                    cardLayout.show(this.getParent(), "businessPanel");
                    // 清空当前登录界面输入
                    clearLogin();
                }
            } catch (NotFoundException | LoginExcepion ex) {  // 处理自定义异常
                JOptionPane.showMessageDialog(this, ex.getMessage(), "登录失败", JOptionPane.ERROR_MESSAGE);
                passwordField.setText("");  // 清空密码框
            }*/

            //1.发送指令
            atm.pw.println("login#"+id+"#"+password);
            atm.pw.flush();

            //4.读服务器传回的结果
            try {
                Object o = atm.ois.readObject();
                if(o instanceof Account){
                    //登录成功 -> 跳转业务界面，初始化业务界面
                    Account acc = (Account)o;
                    if(acc!=null){
                        JOptionPane.showMessageDialog(this , "登录成功");
                        //直接跳转至BusinessPanel界面
                        CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
                        cardLayout.show(this.getParent(),"businessPanel");
                        //初始化BusinessPanel界面数据
                        atm.acc = acc;
                        atm.businessPanel.initBusiness();
                        //清空登录界面
                        clearLogin();
                    }
                }else if(o instanceof String){
                    //登录失败  ->  弹出异常信息，并清空密码框
                    JOptionPane.showMessageDialog(this , o);
                    passwordField.setText("");
                }
            } catch (SocketException ex){
                JOptionPane.showMessageDialog(this , "服务器已关闭");
                System.exit(0);
            } catch (IOException | ClassNotFoundException ex) {
                throw new RuntimeException(ex);
            }
        } else {
            JOptionPane.showMessageDialog(this, "账户不能为空");
        }
    }
    // 清空登录界面输入（账号和密码框）
    public void clearLogin() {
        idField.setText("");
        passwordField.setText("");
    }




    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        titlelabel = new JLabel();
        Backbutton = new JButton();
        idField = new JTextField();
        idlabel = new JLabel();
        passwordField = new JPasswordField();
        JButton OKbutton = new JButton();
        pwdlabel = new JLabel();

        //======== this ========
        setLayout(null);

        //---- titlelabel ----
        titlelabel.setText("\u767b \u5f55 \u754c \u9762");
        titlelabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 20));
        add(titlelabel);
        titlelabel.setBounds(190, 45, 105, 40);

        //---- Backbutton ----
        Backbutton.setText("\u53d6\u6d88");
        Backbutton.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        Backbutton.addActionListener(e -> Back(e));
        add(Backbutton);
        Backbutton.setBounds(295, 360, 78, 34);
        add(idField);
        idField.setBounds(240, 125, 145, 34);

        //---- idlabel ----
        idlabel.setText("\u7528\u6237\u8d26\u53f7\uff1a");
        idlabel.setHorizontalAlignment(SwingConstants.CENTER);
        idlabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(idlabel);
        idlabel.setBounds(120, 125, 90, 30);
        add(passwordField);
        passwordField.setBounds(240, 220, 145, 34);

        //---- OKbutton ----
        OKbutton.setText("\u786e\u8ba4");
        OKbutton.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        OKbutton.addActionListener(e -> OK(e));
        add(OKbutton);
        OKbutton.setBounds(105, 360, 78, 34);

        //---- pwdlabel ----
        pwdlabel.setText("\u7528\u6237\u5bc6\u7801\uff1a");
        pwdlabel.setHorizontalAlignment(SwingConstants.CENTER);
        pwdlabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(pwdlabel);
        pwdlabel.setBounds(120, 220, 90, 30);

        setPreferredSize(new Dimension(500, 600));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel titlelabel;
    private JButton Backbutton;
    private JTextField idField;
    private JLabel idlabel;
    private JPasswordField passwordField;
    private JLabel pwdlabel;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
